/**
 * 
 */
package org.alipay4j.component.life;

import java.util.HashMap;
import java.util.Map;

import org.alipay4j.Alipay;
import org.alipay4j.AlipayException;
import org.alipay4j.Configuration;
import org.alipay4j.component.AbstractComponent;
import org.alipay4j.http.AlipayHttpsClient;
import org.alipay4j.http.Response;
import org.alipay4j.util.ObjectHelper;

import com.alibaba.fastjson.JSONObject;

/**
 * @author heliang
 * 场景操作组件
 */
public class SceneComponent extends AbstractComponent {

	public SceneComponent(Alipay alipay) {
		super(alipay);
	}
	
	/**
	 * 服务窗短链自主生成接口:商户通过本接口生成带自有场景标识的短链接
	 * @param scene_id  短链接对应的场景ID，该ID由商户自己定义
	 * @param remark 对于场景ID的描述，商户自己定义
	 * @return shortlink 短连接
	 * @throws AlipayException
	 */
	public String shortlinkCreate(String scene_id,String remark) throws AlipayException {
		if (ObjectHelper.isEmpty(scene_id)) {
            throw new IllegalArgumentException("scene_id can't be null or empty");
        }
		Map<String, String> body = new HashMap<String, String>();
		body.put("scene_id", scene_id);
		body.put("remark", remark);
		body.put("method", "alipay.open.public.shortlink.create");
		Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
		//根据请求结果判定，是否验证成功
		JSONObject bodyRes = getObjRes(response, "alipay_open_public_shortlink_create_response");
    	if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_shortlink_create_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
        	String sub_code = bodyRes.getString("sub_code");
        	String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
		return bodyRes.getString("shortlink");
	}
	
}
